package kr.or.kead.module;

import java.awt.FlowLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.StringTokenizer;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

@SuppressWarnings("serial")
public class RegPhone extends JPanel {
	private JComboBox<String> comboFirstNum;
	private JLabel lblNewLabel;
	private JTextField txtSecond;
	private JLabel lblNewLabel_1;
	private JTextField txtThird;
	private final String[] mobile = {"010","011","016","017","019"};
	private final String[] telNum={
		"02", "031", "032", "033", "041", "042", "043", "044",
		"051", "052", "053", "054", "055", "061", "062", "063", "064"
	};
	public RegPhone(int Flag) {	
		init(Flag);
	}

	private void init(int Flag) {
		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		if(Flag == 1) {
			comboFirstNum = new JComboBox<>(mobile);
		}else {
			comboFirstNum = new JComboBox<>(telNum);
		}		
		add(comboFirstNum);
		
		lblNewLabel = new JLabel("-");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		add(lblNewLabel);
		
		txtSecond = new JTextField();
		txtSecond.setHorizontalAlignment(SwingConstants.CENTER);
		txtSecond.addFocusListener(new SecondNumFocusListener());
		txtSecond.addKeyListener(new SecondNumKeyListener());
		add(txtSecond);
		txtSecond.setColumns(4);
		
		lblNewLabel_1 = new JLabel("-");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		add(lblNewLabel_1);
		
		txtThird = new JTextField();
		txtThird.setHorizontalAlignment(SwingConstants.CENTER);
		txtThird.addFocusListener(new ThirdFocusListener());
		txtThird.addKeyListener(new ThirdKeyListener());
		add(txtThird);
		txtThird.setColumns(4);	
	}
	
	public void setPhone(String phoneNum) {
		StringTokenizer st = new StringTokenizer(phoneNum, "-");
		comboFirstNum.setSelectedItem(st.nextToken());
		txtSecond.setText(st.nextToken());
		txtThird.setText(st.nextToken());
	}
	
	public String getPhoneNum() {
		return comboFirstNum.getSelectedItem() + txtSecond.getText() + txtThird.getText();
	}
	
	
	
	
	private class SecondNumFocusListener extends FocusAdapter {
		public void focusLost(FocusEvent e) {
			try {
				Integer.parseInt(txtSecond.getText());
			} catch (NumberFormatException ee) {
				JOptionPane.showMessageDialog(null, "숫자만 입력");
				txtSecond.setSelectionStart(0);
				txtSecond.requestFocus();
			}
		}		
	}
	
	private class ThirdFocusListener extends FocusAdapter {
		public void focusLost(FocusEvent e) {
			try {
				Integer.parseInt(txtThird.getText());
			} catch (NumberFormatException ee) {
				JOptionPane.showMessageDialog(null, "숫자만 입력");
				txtThird.setSelectionStart(0);
				txtThird.requestFocus();
			}
		}		
	}
	
	private class SecondNumKeyListener extends KeyAdapter {
		public void keyReleased(KeyEvent e) {
			int len = txtSecond.getText().length();
			if (len == 4) {
				txtThird.requestFocus();
			}
		}		
	}
	
	private class ThirdKeyListener extends KeyAdapter {
		public void keyReleased(KeyEvent e) {
			int len = txtThird.getText().length();
			if (len > 4) {
				JOptionPane.showMessageDialog(null, "숫자 7자리만 입력");
				txtThird.requestFocus();
			}
		}		
	}

}
